<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWechatUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('wechat_users', function (Blueprint $table) {
            $table->id();
            $table->string('openid', 100)->unique()->comment('微信openid');
            $table->string('session_key', 100)->nullable()->comment('微信session_key');
            $table->string('headimgurl', 255)->nullable()->comment('头像');
            $table->string('unionid', 100)->nullable()->unique()->comment('微信unionid');
            $table->string('nickname', 100)->nullable()->comment('微信昵称');
            $table->string('avatar', 500)->nullable()->comment('头像地址');
            $table->tinyInteger('gender')->default(0)->comment('性别：0未知，1男，2女');
            $table->string('country', 50)->nullable()->comment('国家');
            $table->string('province', 50)->nullable()->comment('省份');
            $table->string('city', 50)->nullable()->comment('城市');
            $table->string('language', 20)->default('zh_CN')->comment('语言');
            $table->string('phone', 20)->nullable()->comment('手机号');
            $table->string('email', 100)->nullable()->comment('邮箱');
            $table->timestamp('subscribe_time')->nullable()->comment('关注时间');
            $table->timestamp('unsubscribe_time')->nullable()->comment('取消关注时间');
            $table->tinyInteger('subscribe_status')->default(1)->comment('关注状态：0未关注，1已关注');
            $table->string('remark', 200)->nullable()->comment('备注');
            $table->text('privileges')->nullable()->comment('用户特权信息(JSON格式)');
            $table->timestamps();
            $table->softDeletes();
            
            // 索引
            $table->index(['openid']);
            $table->index(['unionid']);
            $table->index(['phone']);
            $table->index(['subscribe_status']);
            $table->index(['created_at']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('wechat_users');
    }
}